home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14546 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: mayne.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Check if a file exists?
  5. Date: 15 Apr 1996 11:32:19 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4ku4njINNmse@mayne.ugrad.cs.ubc.ca>
  8. References: <4kp7pg$upe@news-s01.ny.us.ibm.net> <4ktl40$b76@texas.nwlink.com>
  9. NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
  10.  
  11. In article <4ktl40$b76@texas.nwlink.com>,
  12. Teresa Reiko  <tjr19@mail.nwlink.com> wrote:
  13. >bfilone@ibm.net  <Bruce Filone> wrote:
  14. >>Is it possible in C to check if a file exists other than checking for a >succesful 
  15. >>fopen? ON a unix machine, I just want to know if a file is there or not, >do not
  16. >>to read or write anything to it. similar to the -f test in unix? I was >hoping for
  17. >>something less costly than opening and closing if exists because I am >checking
  18. >>for the existance of many files during the run of a process 
  19. >>Did not see any postings that seemed to refer to this.
  20. >
  21. >Use rename():
  22. >
  23. >You could try to rename it, and if that fails it's not there,
  24. >if it works it exists, and then you can rename it back.
  25.  
  26. Then you need a good temporary name from tmpnam() to make sure you don't
  27. clobber an existing file. There are potential race conditions in the use of
  28. tmpnam(): you have to check that the name does not, in fact, exist, before you
  29. use it, which leads you to circular regress.
  30.  
  31. >Or, you can create a temporary file, try to rename it to the
  32. >name of the file you're testing, if it fails it exists, and
  33.  
  34. Even if you could do this using strictly ANSI/ISO C functionality, on a UNIX
  35. system, renaming the temporary file to the name of the file you are testing is
  36. deadly. It _always_ succeeds (modulo the requisite permissions, and acts of
  37. God). If the original file existed, it is _gone_!
  38.  
  39. >if it works it wasn't there, and then you can rename it back.
  40.  
  41. No you can't! :)
  42.  
  43. And anyway, ANSI/ISO C doesn't give you a way to create a temporary file whose
  44. name you *know* for the purposes of renaming.
  45.  
  46. The tmpfile() standard function returns an open file pointer. There is no
  47. standard mechanism to access the name of this file for the purpose of renaming
  48. another file to it. So to do the (destructive) operation you describe, you
  49. would have to rely on UNIX- or BSD-isms like mkstemp(), which makes a temporary
  50. file _and_ opens it in one step. Or you would have to do a spin loop, whereby
  51. you keep trying to make up temporary names until you get one that is not in
  52. use. And that requires an existence check, of course...
  53.  
  54. >This should be faster than fopen, but you would need to have
  55. >write access to all the files you're testing.
  56.  
  57. Why would any of this be faster than a simple fopen?
  58.